﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using AgileBoard.Domain;
using AgileBoard.Dtos;
using AgileBoard.Dtos.Mappers;
using AgileBoard.Web.Controllers.Util;

namespace AgileBoard.Web.Controllers
{
    public class IterationController : BaseController
    {
        // POST: /Iteration/Index/ReleaseID

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Index(Guid releaseId)
        {
            var rdto = new ResultDTO();

            IList iterations = ManagerFactory.IterationManager.GetByRelease(releaseId);

            IList<IterationDTO> iterationDTO = new List<IterationDTO>();
            foreach (Iteration iteration in iterations)
            {
                IterationDTO iterationDto = IterationDTOMapper.MapToDTO(iteration);
                iterationDTO.Add(iterationDto);
            }

            rdto.Data = iterationDTO.ToExtJsJson("items", "totalCount");
            rdto.Result = true;
            rdto.Message = "Success";

            return Json(rdto);
        }

        // POST: /Iteration/Add/Json, Update

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Add(IterationDTO Json, Boolean Update)
        {
            var rdto = new ResultDTO();
            Iteration newIteration = IterationDTOMapper.MapFromDTO(Json);
            DateTime labelDate = newIteration.PeriodTo;
            Release release = ManagerFactory.ReleaseManager.Get(newIteration.ReleaseID);

            if (!Update)
            {
                if (DateTime.Compare(DateTime.MinValue.Date, newIteration.PeriodTo.Date) == 0)
                {
                    newIteration.PeriodFrom = release.PeriodFrom;
                }
                else if (newIteration.PeriodTo.AddDays(1).Date <= release.PeriodTo.Date)
                {
                    newIteration.PeriodFrom = newIteration.PeriodTo.AddDays(1);
                }
                else
                    newIteration.PeriodFrom = release.PeriodTo;

                //initialize new release default data
                newIteration.Name = "New Iteration";
                newIteration.CreateTime = DateTime.Now;

                if (newIteration.PeriodFrom.AddDays(13).Date <= release.PeriodTo.Date)
                    newIteration.PeriodTo = newIteration.PeriodFrom.AddDays(13);
                else
                    newIteration.PeriodTo = release.PeriodTo;
            }

            try
            {
                if (Update && DateTime.Compare(newIteration.PeriodFrom.Date, release.PeriodFrom.Date) < 0
                    || DateTime.Compare(newIteration.PeriodTo.Date, release.PeriodTo.Date) > 0
                    )
                {
                    rdto.Message = "Iteration DateRange should be within Release period.";
                    rdto.Result = false;
                    return this.Json(rdto);
                }

                newIteration = ManagerFactory.IterationManager.Update(newIteration);
                if (!Update)
                {
                    labelDate = newIteration.PeriodFrom;
                    for (int i = 1; i < 15; i++)
                    {
                        if (i != 1) labelDate = labelDate.AddDays(1);
                        Label label = ManagerFactory.LabelManager.NewLabel();

                        label.Date = labelDate.ToShortDateString();
                        label.IterationID = newIteration.IterationID.ToString();
                        ManagerFactory.LabelManager.Save(label);
                    }
                }
                rdto.Message = "Success";
                rdto.Result = true;
                rdto.Data = IterationDTOMapper.MapToDTO(newIteration).ToExtJsJson();
                ;
            }
            catch (Exception e)
            {
                rdto.Message = e.Message;
                rdto.Result = false;
            }

            return this.Json(rdto);
        }

        // POST: /Iteration/Delete/GUID

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Delete(Guid Guid)
        {
            var rdto = new ResultDTO();
            try
            {
                IList Tasks = ManagerFactory.TaskManager.GetByIteration(Guid.ToString());
                foreach (Task task in Tasks)
                {
                    ManagerFactory.TaskManager.Delete(task.TaskID);
                }
                ManagerFactory.IterationManager.Delete(Guid);
                rdto.Message = "Success";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = ex.Message;
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}